package me.zhouzhuo810.magpiex.ui.dialog.adapter;

import android.content.Context;
import android.view.Gravity;
import android.widget.TextView;

import java.io.File;
import java.util.List;

import me.zhouzhuo810.magpiex.R;
import me.zhouzhuo810.magpiex.ui.adapter.RvBaseAdapter;
import me.zhouzhuo810.magpiex.ui.widget.intef.IListDialogProvider;

/**
 * 列表对话框适配器
 *
 * @author zhouzhuo810
 * @date 6/15/21 1:53 PM
 */
public class ListDialogAdapter<T> extends RvBaseAdapter<T> {
    
    private boolean alignLeft;
    private boolean landscape;
    
    public boolean isAlignLeft() {
        return alignLeft;
    }
    
    public void setAlignLeft(boolean alignLeft) {
        this.alignLeft = alignLeft;
    }
    
    public boolean isLandscape() {
        return landscape;
    }
    
    public void setLandscape(boolean landscape) {
        this.landscape = landscape;
    }
    
    public ListDialogAdapter(Context context, List<T> data, boolean landscape) {
        super(context, data);
        this.landscape = landscape;
    }
    
    @Override
    protected int getLayoutId(int viewType) {
        return landscape ? R.layout.item_lv_dialog_land : R.layout.item_lv_dialog;
    }
    
    @Override
    protected void fillData(ViewHolder holder, T item, int position) {
        TextView view = holder.getView(R.id.tv_name);
        if (item instanceof IListDialogProvider) {
            view.setText(((IListDialogProvider) item).getShowContent());
        } else if (item instanceof CharSequence) {
            view.setText((CharSequence) item);
        } else if (item instanceof File) {
            view.setText(((File) item).getName());
        } else {
            throw new RuntimeException("showListDialog 范型 必须是 CharSequence 或 CharSequence的子类 或者 File 或者 IListDialogProvider 的子类");
        }
        holder.itemView.setBackgroundResource(R.drawable.item_bg_selector);
        holder.setVisible(R.id.line_item, position != 0);
        if (alignLeft) {
            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
        } else {
            view.setGravity(Gravity.CENTER);
        }
    }
}
